package com.example.demo.config;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.params.shadow.com.univocity.parsers.csv.CsvWriter;
import org.junit.jupiter.params.shadow.com.univocity.parsers.csv.CsvWriterSettings;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.io.ICsvMapWriter;
import org.supercsv.prefs.CsvPreference;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class test {

    private static boolean boo;
    String a = "dd"+"ddd"+ System.currentTimeMillis();

    public static void main(String[] args) {

        //testAsset(false);

        postHttp(postFromParameters());
        /*try {
            writeWithCsvMapWriter();
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }

    private static void testAsset(boolean is){
        log.info("arg :{}",is);
        assert is;
        log.info("here :{}",is);
    }
    /**
     * An example of reading using CsvMapWriter.
     */
    private static void writeWithCsvMapWriter() throws Exception {

        int times = 1000;
        ICsvMapWriter mapWriter = null;
        try {
            mapWriter = new CsvMapWriter(new FileWriter("test.csv"),
                    CsvPreference.STANDARD_PREFERENCE);

            String[] header = new String[]{"pid","serviceName","timestamp","bizContent","sign"};

            // write the header
            mapWriter.writeHeader(header);

            // write the customer maps
            for (int i = 0; i < times; i++) {
                mapWriter.write(postFromParameters(), header);
            }


        }
        finally {
            if( mapWriter != null ) {
                mapWriter.close();
            }
        }
    }
    public static void write(){
        String filePath = /*"D:" + File.separator + */"test.csv";
       /* if (!new File(filePath).exists()){
            new File(filePath).mkdir();
        }*/
        int times = 1;
        try {
            // 创建CSV写对象
            CsvWriter csvWriter = new CsvWriter(new File(filePath), "utf-8", new CsvWriterSettings());
            //CsvWriter csvWriter = new CsvWriter(filePath);

            // 写表头
            csvWriter.writeHeaders("context");

            for (int i = 0; i < times; i++) {
                csvWriter.writeRow(postFromParameters());

            }

            csvWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    private List<Map<String, String>> recordList = new ArrayList<>();

    @Before
    public void init() {
        for (int i = 0; i < 5; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("name", "zhangsan");
            map.put("code", "001");
            recordList.add(map);
        }
    }


    static String url = "http://127.0.0.1:8004/open/api"; // 请求链接
    static String url11 = "http://127.0.0.1:8004/php/recognize"; // 请求链接
    static String url1 = "http://127.0.0.1:8004/php/modifyCar"; // 请求链接
    static String KEY = "29bfd42e5a753522b2e77f3071901f98"; // 请求参数
    /**
     * Post提交表单
     */
    public static  HashMap<String ,Object>  postFromParameters() {

        HashMap<String ,Object> hashMap = new HashMap<>();
        hashMap.put("pid","20187834654");
        hashMap.put("serviceName","discountNotify");
        //hashMap.put("serviceName","monthCardProducts");
        //hashMap.put("serviceName","enterSync");
        //hashMap.put("serviceName","renewMonthCard");
        hashMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
        B1 b1 = new B1();
        b1.setPlateNum("黑P1JQ55");
        b1.setParkCode("P157189329910530");
        B2 b2 = new B2();
        b2.setParkCode("P1542006648");

        RecognizeRequest recognizeRequest = new RecognizeRequest();
        recognizeRequest.setOrderNum("20032710330442375027");
        recognizeRequest.setNewCarType(1);
        recognizeRequest.setNewPlateNum("澜 A123456");
        recognizeRequest.setNewReliability(100);

        ModifyCarRequest modifyCarRequest = new ModifyCarRequest();
        modifyCarRequest.setNewCarType(1);
        modifyCarRequest.setNewPlateNum("ddddddd");
        modifyCarRequest.setOrderNum("20032710330442375027");


        RenewMonthCardRequest renewMonthCardRequest = new RenewMonthCardRequest();
        renewMonthCardRequest.setParkCode("P1540450791");
        renewMonthCardRequest.setCardId(10);
        renewMonthCardRequest.setProductId(15);
        renewMonthCardRequest.setCardOwner("51213");
        renewMonthCardRequest.setPlateNum("");
        renewMonthCardRequest.setPhone("15256589856");
        renewMonthCardRequest.setPlotCount(2);
        renewMonthCardRequest.setPlotNum("");
        renewMonthCardRequest.setStartTime("2020-06-20");
        renewMonthCardRequest.setEndTime("2020-09-20");

        NotifyOrderDiscountRequest notifyOrderDiscountRequest = new NotifyOrderDiscountRequest();
        notifyOrderDiscountRequest.setParkCode("P1542006648");
        notifyOrderDiscountRequest.setOutDiscountNo("1254789"+ new Random().nextInt(9999));
        notifyOrderDiscountRequest.setDiscountNumber(String.valueOf(new Random().nextInt(9999)));
        notifyOrderDiscountRequest.setDiscountTime(System.currentTimeMillis());
        notifyOrderDiscountRequest.setOrderNum("19052713384205845"+new Random().nextInt(9999));
        notifyOrderDiscountRequest.setDiscountType(new Random().nextInt(4));
        notifyOrderDiscountRequest.setPlateNum("d445544500"+new Random().nextInt(9999));


        NotifyParkCarEnterRequest notifyParkCarEnterRequest = new NotifyParkCarEnterRequest();
        notifyParkCarEnterRequest.setPlateNum("大铁锤");
        notifyParkCarEnterRequest.setChannelCode("C154200948580767");
        notifyParkCarEnterRequest.setParkCode("P1542006648");
        notifyParkCarEnterRequest.setEnterTime(System.currentTimeMillis()/1000L);
        long timeNow = System.currentTimeMillis();


        hashMap.put("bizContent",JSON.toJSON(modifyCarRequest));
        String sign = MD5Util.getMD5String(SignTools.getSignContentMap(hashMap,KEY)).toUpperCase();
        log.info("sing===>{}",sign);
        hashMap.put("sign",sign);
        String json = JSON.toJSONString(hashMap);
        log.info("耗时时间  :{}",System.currentTimeMillis()-timeNow);
        log.info("结果 json {}",json);
        return hashMap;
    }

    private static void postHttp(HashMap<String ,Object>  hashMap ){
        String json = JSON.toJSONString(hashMap);
        OkHttpClient okHttpClient = new OkHttpClient(); // OkHttpClient对象
        MediaType mediaType = MediaType.parse("application/json");

        RequestBody formBody = FormBody.create(mediaType,json);
        log.info(json);
        Request request = new Request.Builder().url(url1).post(formBody).build(); // 请求
        okHttpClient.newCall(request).enqueue(new Callback() {// 回调

            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body().string());//成功后的回调
            }

            public void onFailure(Call call, IOException e) {
                System.out.println(e.getMessage());//失败后的回调
            }
        });

    }

    static class B2{
        String parkCode;

        public String getParkCode() {
            return parkCode;
        }

        public void setParkCode(String parkCode) {
            this.parkCode = parkCode;
        }
    }

    static class B1{
        String parkCode;
        String plateNum;
        String channelCode;
        String orderNum;
        String cardNo;
        String deductionDetails;

        public String getParkCode() {
            return parkCode;
        }

        public void setParkCode(String parkCode) {
            this.parkCode = parkCode;
        }

        public String getPlateNum() {
            return plateNum;
        }

        public void setPlateNum(String plateNum) {
            this.plateNum = plateNum;
        }

        public String getChannelCode() {
            return channelCode;
        }

        public void setChannelCode(String channelCode) {
            this.channelCode = channelCode;
        }

        public String getOrderNum() {
            return orderNum;
        }

        public void setOrderNum(String orderNum) {
            this.orderNum = orderNum;
        }

        public String getCardNo() {
            return cardNo;
        }

        public void setCardNo(String cardNo) {
            this.cardNo = cardNo;
        }

        public String getDeductionDetails() {
            return deductionDetails;
        }

        public void setDeductionDetails(String deductionDetails) {
            this.deductionDetails = deductionDetails;
        }
    }
}
